package com.fm.parser.zip;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

/**
 * @author leo.li
 */
public class ZipReader {
    public static void unZip(File file, ZipHandler handler) throws IOException {
        unZip(file, StandardCharsets.UTF_8, handler);
    }

    public static void unZip(File file, Charset charset, ZipHandler handler) throws IOException {
        ZipFile zfile = new ZipFile(file, ZipFile.OPEN_READ, charset);
        try(ZipInputStream zin = new ZipInputStream(new FileInputStream(file), charset)) {
            ZipEntry entry;
            while ((entry = zin.getNextEntry()) != null) {
                handler.doEntry(entry, zfile.getInputStream(entry));
            }
        }
    }
}
